package copyengine.utils.commandQueue
{

	public final class CECommandExecuteQueue
	{
		private var subExecuteWarpVector:Vector.<CECommandExecuteWarp>;

		//在beginFill~endFill期间标志当前操作的Warp
		//在执行期间表示执行时候的Warp
		private var currentExecuteWarp:CECommandExecuteWarp;

		private var endCallBackFunction:Function;

		private var uuid:String

		public function CECommandExecuteQueue(_uuid:String)
		{
			uuid = _uuid;
			subExecuteWarpVector = new Vector.<CECommandExecuteWarp>();
		}

		public function dispose():void
		{
			subExecuteWarpVector.length = 0;
			subExecuteWarpVector = null;
			currentExecuteWarp = null;
			endCallBackFunction = null;
			uuid = null;
		}

		public function setEndCallBackFunction(_callBackFunction:Function):void
		{
			endCallBackFunction = _callBackFunction;
		}

		public function beginFillStep():void
		{
			nextStep();
		}

		public function nextStep():void
		{
			if (currentExecuteWarp)
			{
				subExecuteWarpVector.push(currentExecuteWarp);
			}
			currentExecuteWarp = new CECommandExecuteWarp();
			currentExecuteWarp.initialize(onCurrentExecuterWarpFinish);
		}

		public function endFillCommandStep():void
		{
			subExecuteWarpVector.push(currentExecuteWarp);
			//endFill结束 清除当前引用，以后再用用到引用为执行阶段引用
			currentExecuteWarp = null;
		}

		public function addExecuter(_executer:ICECommandExecuter):void
		{
			_executer.setExecuteWarpUUID(uuid);
			currentExecuteWarp.addExecuter(_executer);
		}

		public function startExecuter():void
		{
			startToExecuteNext();
		}

		public function getUUID():String
		{
			return uuid;
		}

		public function onExecuterFinish():void
		{
			currentExecuteWarp.onExecuterFinish();
		}

		private function onCurrentExecuterWarpFinish():void
		{
			startToExecuteNext();
		}

		private function startToExecuteNext():void
		{
			if (currentExecuteWarp)
			{
				currentExecuteWarp.dispose();
			}
			currentExecuteWarp = subExecuteWarpVector.shift();
			if (currentExecuteWarp)
			{
				currentExecuteWarp.startExecute();
			}
			else
			{
				endCallBackFunction && endCallBackFunction();
				CECommandQueueManger.instance.onExecuteQueueFinish(this);
			}
		}

	}
}
